package com.git.hui.yinshua.biz.rest.admin.user;

import com.git.hui.yinshua.api.model.ExceptionUtil;
import com.git.hui.yinshua.api.model.ListRsp;
import com.git.hui.yinshua.api.model.ReqInfoContext;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.api.model.user.UserBo;
import com.git.hui.yinshua.biz.rest.models.user.req.UserAddReq;
import com.git.hui.yinshua.biz.rest.models.user.req.UserSearchReq;
import com.git.hui.yinshua.biz.rest.models.user.req.UserUpdateReq;
import com.git.hui.yinshua.biz.rest.models.user.res.UserInfoAddrDto;
import com.git.hui.yinshua.biz.rest.models.user.res.UserInfoDto;
import com.git.hui.yinshua.biz.rest.models.user.res.UserLoginDto;
import com.git.hui.yinshua.biz.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author YiHui
 * @date 2024/6/12
 */
@RequestMapping(path = "admin/user")
@RestController
public class AdminUserController {
    @Autowired
    private UserService userService;


    /**
     * 新增用户
     *
     * @param addReq
     * @return
     */
    @RequestMapping(path = "create")
    @Permission(role = UserAccessRole.SUPER_ADMIN)
    public ResVo<UserInfoDto> createUser(@RequestBody UserAddReq addReq) {
        if (StringUtils.isBlank(addReq.getUname()) || StringUtils.isBlank(addReq.getPwd())) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "用户名/密码缺失");
        }

        return ResVo.ok(userService.createUser(addReq));
    }

    /**
     * 管理员登录
     *
     * @param uname 用户名
     * @param pwd   密码
     * @return
     */
    @RequestMapping(path = "login")
    public ResVo<UserLoginDto> login(String uname, String pwd) {
        if (ReqInfoContext.getReqInfo().getUserId() != null) {
            // 已经登录了，无需再次登录
            UserLoginDto vo = new UserLoginDto();
            vo.setUser(ReqInfoContext.getReqInfo().getUser());
            vo.setToken(ReqInfoContext.getReqInfo().getSessionId());
            return ResVo.ok(vo);
        }

        // 账密方式登录
        UserLoginDto vo = userService.pwdLogin(uname, pwd);
        return ResVo.ok(vo);
    }

    @GetMapping(path = "logout")
    @Permission(role = UserAccessRole.LOGIN)
    public ResVo<Boolean> logout() {
        return ResVo.ok(userService.logout());
    }

    /**
     * 用户列表
     *
     * @param page
     * @return
     */
    @GetMapping(path = "list")
    @Permission(role = UserAccessRole.ADMIN)
    public ResVo<ListRsp<UserInfoAddrDto>> list(UserSearchReq page) {
        if (StringUtils.isBlank(page.getUname())) {
            page.setUname(null);
        }
        if (StringUtils.isBlank(page.getNickname())) {
            page.setNickname(null);
        }
        if (StringUtils.isBlank(page.getPhone())) {
            page.setPhone(null);
        }
        page.autoInitReq();
        ListRsp<UserInfoAddrDto> list = userService.listUser(page);
        return ResVo.ok(list);
    }

    /**
     * 更新密码
     *
     * @param pwd
     * @param newPwd
     * @return
     */
    @PostMapping(path = "updatePwd")
    @Permission(role = UserAccessRole.ADMIN)
    public ResVo<UserInfoDto> updatePwd(String pwd, String newPwd) {
        UserBo user = ReqInfoContext.getReqInfo().getUser();
        return ResVo.ok(userService.updatePwd(user.getUserName(), pwd, newPwd));
    }


    /**
     * 超管，更新其他用户的基本信息
     *
     * @param req
     * @return
     */
    @PostMapping(path = "updateUser")
    @Permission(role = UserAccessRole.ADMIN)
    public ResVo<UserInfoDto> updateUserInfo(@RequestBody UserUpdateReq req) {
        if (req.getUid() == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "用户id不能为空");
        }

        return ResVo.ok(userService.updateUser(req));
    }
}
